package com.chengxusheji.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.chengxusheji.utils.ExportExcelUtil;
import com.chengxusheji.dao.CardTypeDAO;
import com.chengxusheji.domain.CardType;
@Controller @Scope("prototype")
public class CardTypeAction extends ActionSupport {

    /*当前第几页*/
    private int currentPage;
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getCurrentPage() {
        return currentPage;
    }

    /*一共多少页*/
    private int totalPage;
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }

    private int typeId;
    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }
    public int getTypeId() {
        return typeId;
    }

    /*当前查询的总记录数目*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    /*业务层对象*/
    @Resource CardTypeDAO cardTypeDAO;

    /*待操作的CardType对象*/
    private CardType cardType;
    public void setCardType(CardType cardType) {
        this.cardType = cardType;
    }
    public CardType getCardType() {
        return this.cardType;
    }

    /*跳转到添加CardType视图*/
    public String AddView() {
        ActionContext ctx = ActionContext.getContext();
        return "add_view";
    }

    /*添加CardType信息*/
    @SuppressWarnings("deprecation")
    public String AddCardType() {
        ActionContext ctx = ActionContext.getContext();
        try {
            cardTypeDAO.AddCardType(cardType);
            ctx.put("message",  java.net.URLEncoder.encode("CardType添加成功!"));
            return "add_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CardType添加失败!"));
            return "error";
        }
    }

    /*查询CardType信息*/
    public String QueryCardType() {
        if(currentPage == 0) currentPage = 1;
        List<CardType> cardTypeList = cardTypeDAO.QueryCardTypeInfo(currentPage);
        /*计算总的页数和总的记录数*/
        cardTypeDAO.CalculateTotalPageAndRecordNumber();
        /*获取到总的页码数目*/
        totalPage = cardTypeDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = cardTypeDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("cardTypeList",  cardTypeList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        return "query_view";
    }

    /*后台导出到excel*/
    public String QueryCardTypeOutputToExcel() { 
        List<CardType> cardTypeList = cardTypeDAO.QueryCardTypeInfo();
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "CardType信息记录"; 
        String[] headers = { "记录编号","类别名称"};
        List<String[]> dataset = new ArrayList<String[]>(); 
        for(int i=0;i<cardTypeList.size();i++) {
        	CardType cardType = cardTypeList.get(i); 
        	dataset.add(new String[]{cardType.getTypeId() + "",cardType.getTypeName()});
        }
        /*
        OutputStream out = null;
		try {
			out = new FileOutputStream("C://output.xls");
			ex.exportExcel(title,headers, dataset, out);
		    out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
		HttpServletResponse response = null;//创建一个HttpServletResponse对象 
		OutputStream out = null;//创建一个输出流对象 
		try { 
			response = ServletActionContext.getResponse();//初始化HttpServletResponse对象 
			out = response.getOutputStream();//
			response.setHeader("Content-disposition","attachment; filename="+"CardType.xls");//filename是下载的xls的名，建议最好用英文 
			response.setContentType("application/msexcel;charset=UTF-8");//设置类型 
			response.setHeader("Pragma","No-cache");//设置头 
			response.setHeader("Cache-Control","no-cache");//设置头 
			response.setDateHeader("Expires", 0);//设置日期头  
			String rootPath = ServletActionContext.getServletContext().getRealPath("/");
			ex.exportExcel(rootPath,title,headers, dataset, out);
			out.flush();
		} catch (IOException e) { 
			e.printStackTrace(); 
		}finally{
			try{
				if(out!=null){ 
					out.close(); 
				}
			}catch(IOException e){ 
				e.printStackTrace(); 
			} 
		}
		return null;
    }
    /*前台查询CardType信息*/
    public String FrontQueryCardType() {
        if(currentPage == 0) currentPage = 1;
        List<CardType> cardTypeList = cardTypeDAO.QueryCardTypeInfo(currentPage);
        /*计算总的页数和总的记录数*/
        cardTypeDAO.CalculateTotalPageAndRecordNumber();
        /*获取到总的页码数目*/
        totalPage = cardTypeDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = cardTypeDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("cardTypeList",  cardTypeList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        return "front_query_view";
    }

    /*查询要修改的CardType信息*/
    public String ModifyCardTypeQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键typeId获取CardType对象*/
        CardType cardType = cardTypeDAO.GetCardTypeByTypeId(typeId);

        ctx.put("cardType",  cardType);
        return "modify_view";
    }

    /*查询要修改的CardType信息*/
    public String FrontShowCardTypeQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键typeId获取CardType对象*/
        CardType cardType = cardTypeDAO.GetCardTypeByTypeId(typeId);

        ctx.put("cardType",  cardType);
        return "front_show_view";
    }

    /*更新修改CardType信息*/
    public String ModifyCardType() {
        ActionContext ctx = ActionContext.getContext();
        try {
            cardTypeDAO.UpdateCardType(cardType);
            ctx.put("message",  java.net.URLEncoder.encode("CardType信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CardType信息更新失败!"));
            return "error";
       }
   }

    /*删除CardType信息*/
    public String DeleteCardType() {
        ActionContext ctx = ActionContext.getContext();
        try { 
            cardTypeDAO.DeleteCardType(typeId);
            ctx.put("message",  java.net.URLEncoder.encode("CardType删除成功!"));
            return "delete_success";
        } catch (Exception e) { 
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CardType删除失败!"));
            return "error";
        }
    }

}
